import { ApiProperty } from '@nestjs/swagger';
import {
  BeforeInsert,
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  OneToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { Order } from '../../order/assets/order.entity';
import { UserClient } from '../../userClient/assets/userClient.entity';
import {
  Constant_Enum_Enum_Rebates_DistributionStatus,
  Constant_Enum_Enum_Rebates_RebatesType,
  Constant_Enum_Name_Rebates_RebatesType,
  Constant_Enum_Name_Rebates_Status,
  IConstant_Enum_Type_Rebates_DistributionStatus,
  IConstant_Enum_Type_Rebates_RebatesType,
} from '@/common/constants/enum';
import { CashBack } from '../../cashBack/assets/cashBack.entity';

@Entity()
export class Rebates {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({
    default: Constant_Enum_Enum_Rebates_DistributionStatus.InviteRelation_DistributionStatus_Accouting,
  })
  @ApiProperty({
    nullable: true,
    description: '返利状态' + Constant_Enum_Name_Rebates_Status,
    enum: Constant_Enum_Enum_Rebates_DistributionStatus,
  })
  rebatesStatus: IConstant_Enum_Type_Rebates_DistributionStatus;

  @Column()
  @ApiProperty({
    description: '单类' + Constant_Enum_Name_Rebates_RebatesType,
    enum: Constant_Enum_Enum_Rebates_RebatesType,
  })
  rebatesType: IConstant_Enum_Type_Rebates_RebatesType;

  @Column({ type: 'bigint', nullable: true })
  @ApiProperty({ type: 'bigint', description: '返利时间' })
  rebatesTime?: number;

  @Column({ type: 'bigint', nullable: true })
  @ApiProperty({ type: 'bigint', description: '返利金额' })
  rebatesMoney?: number;

  @OneToOne(() => Order)
  @ApiProperty({ description: '订单', type: () => Order })
  @JoinColumn()
  order: Order;

  @ApiProperty({ description: '邀请者', type: () => UserClient })
  @OneToOne(() => UserClient, { cascade: ['remove'], onDelete: 'CASCADE' })
  @JoinColumn()
  inviter: UserClient;

  @ApiProperty({ description: '被邀请者', type: () => UserClient })
  @OneToOne(() => UserClient, { cascade: ['remove'], onDelete: 'SET NULL' })
  @JoinColumn()
  invitee: UserClient;

  @ApiProperty({ description: '提现', type: () => CashBack })
  @ManyToOne(() => CashBack)
  cashBack: CashBack;

  @Column({
    type: 'bigint',
    nullable: true,
  })
  @ApiProperty({ description: '创建时间' })
  createTime: number;

  @BeforeInsert()
  setCreateTime() {
    this.createTime = Math.floor(Date.now() / 1000);
  }
}
